package config

import "GameServer/utils"

type config struct {
	// listen address.
	ListenAddr string

	// log level.
	LogLevel int32

	// start mysql connections
	Host     string
	DBName   string
	User     string
	Passwd   string
	Charset  string
	MaxConn  int
	IdleConn int
	MaxLife  int32 // unit: Minute.
	// end mysql connection.
}

func (c *config) Load(filePath string) error {
	var reader utils.Config
	if err := reader.Parse(filePath); err != nil {
		return err
	}

	// listen address.
	c.ListenAddr = reader.GetNodeString("Main/ListenAddr", ":5566")

	// log level.
	c.LogLevel = reader.GetNodeInt32("Main/LogLevel", 0)

	// mysql connection config.
	var err error
	c.Host, err = reader.GetString("Main/MySQL/DBhost")
	if err != nil {
		return err
	}
	c.DBName, err = reader.GetString("Main/MySQL/DBname")
	if err != nil {
		return err
	}
	c.User, err = reader.GetString("Main/MySQL/DBuser")
	if err != nil {
		return err
	}
	c.Passwd, err = reader.GetString("Main/MySQL/DBpass")
	if err != nil {
		return err
	}
	c.Charset, err = reader.GetString("Main/MySQL/Charset")
	if err != nil {
		return err
	}
	c.MaxConn, err = reader.GetInt("Main/MySQL/MaxConn")
	if err != nil {
		return err
	}
	c.IdleConn, err = reader.GetInt("Main/MySQL/IdleConn")
	if err != nil {
		return err
	}
	c.MaxLife, err = reader.GetInt32("Main/MySQL/MaxLife")
	if err != nil {
		return err
	}
	return nil
}

var ConfigObj config
